﻿using Iservices;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Model;
using EFEntity;
using System.Linq;
using Microsoft.Data.SqlClient;
using AutoMapper;
using Microsoft.EntityFrameworkCore;


namespace Services
{
    public class human_registerServices : Ihuman_registerServices
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;
        public human_registerServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }

        public async Task<int> AddHuman_file(human_file hf)
        {
            int num = 0;
            Ehuman_file egm = new Ehuman_file()
            {
                huf_id = hf.huf_id,
                human_id = hf.human_id,
                first_kind_id = hf.first_kind_id,
                first_kind_name = hf.first_kind_name,
                second_kind_id = hf.second_kind_id,
                second_kind_name = hf.second_kind_name,
                third_kind_id = hf.third_kind_id,
                third_kind_name = hf.third_kind_name,
                human_name = hf.human_name,
                human_address = hf.human_address,
                human_postcode = hf.human_postcode,
                human_pro_designation = hf.human_pro_designation,
                human_major_kind_id = hf.human_major_kind_id,
                human_major_kind_name = hf.human_major_kind_name,
                human_major_id = hf.human_major_id,
                hunma_major_name = hf.hunma_major_name,
                human_telephone = hf.human_telephone,
                human_mobilephone = hf.human_mobilephone,
                human_bank = hf.human_bank,
                human_account = hf.human_account,
                human_qq = hf.human_qq,
                human_email = hf.human_email,
                human_hobby = hf.human_hobby,
                human_speciality = hf.human_speciality,
                human_sex = hf.human_sex,
                human_religion = hf.human_religion,
                human_party = hf.human_party,
                human_nationality = hf.human_nationality,
                human_race = hf.human_race,
                human_birthday = hf.human_birthday,
                human_birthplace = hf.human_birthplace,
                human_age = hf.human_age,
                human_educated_degree = hf.human_educated_degree,
                human_educated_years = hf.human_educated_years,
                human_educated_major = hf.human_educated_major,
                human_society_security_id = hf.human_society_security_id,
                human_id_card = hf.human_id_card,
                remark = hf.remark,
                salary_standard_id = hf.salary_standard_id,
                salary_standard_name = hf.salary_standard_name,
                salary_sum = hf.salary_sum,
                demand_salaray_sum = hf.demand_salaray_sum,
                paid_salary_sum = hf.paid_salary_sum,
                major_change_amount = hf.major_change_amount,
                bonus_amount = hf.bonus_amount,
                training_amount = hf.training_amount,
                file_chang_amount = hf.file_chang_amount,
                human_histroy_records = hf.human_histroy_records,
                human_family_membership = hf.human_family_membership,
                human_picture = hf.human_picture,
                attachment_name = hf.attachment_name,
                check_status = hf.check_status,
                register = hf.register,
                checker = hf.checker,
                changer = hf.changer,
                regist_time = hf.regist_time,
                check_time = hf.check_time,
                change_time = hf.change_time,
                lastly_change_time = hf.lastly_change_time,
                delete_time = hf.delete_time,
                recovery_time = hf.recovery_time,
                human_file_status = hf.human_file_status,
            };
            myDbContext.human_file.Add(egm);
            num = await myDbContext.SaveChangesAsync();
            return num;
        }
        public async Task<int> FHHuman_file(human_file hf)
        {
            int num = 0;
            Ehuman_file ehf = myDbContext.human_file.Find(hf.huf_id);
            ehf.checker = hf.checker;
            ehf.check_time = hf.check_time;
            ehf.human_file_status = hf.human_file_status;
            ehf.check_status = hf.check_status;
            num = await myDbContext.SaveChangesAsync();
            return num;
        }

        /// <summary>
        /// 下拉框查一级机构全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<config_file_first_kind>> GetFirst()
        {
            List<Econfig_file_first_kind> elist = new List<Econfig_file_first_kind>();
            List<config_file_first_kind> list = new List<config_file_first_kind>();
            await Task.Run(() =>
            {
                elist = myDbContext.config_file_first_kind.ToList();
            });
            list = mapper.Map<List<config_file_first_kind>>(elist);
            return list;
        }
        public async Task<Dictionary<string, object>> GetHuman_fileOnes(int currentPage, string where)
        {
            List<Ehuman_file> list = new List<Ehuman_file>();
            Dictionary<string, object> di = new Dictionary<string, object>();
            //调用分页存储过程
            SqlParameter[] ps = {
                new SqlParameter() {ParameterName="@pageSize",Value=3 },
                new SqlParameter() {ParameterName="@keyName",Value="huf_id" },
                new SqlParameter() {ParameterName="@tabelName",Value="human_file" },
                new SqlParameter() {ParameterName="@where",Value="" },
                new SqlParameter() {ParameterName="@currentPage",Value=currentPage },
                new SqlParameter() {ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int },
                new SqlParameter() {ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int }
                };
            list = await myDbContext.human_file.FromSqlRaw
                ("exec proc_FenYe @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output, @pages output", ps)
                     .ToListAsync();
            int rows = (int)ps[5].Value;
            int pages = (int)ps[6].Value;
            List<human_file> list2 = mapper.Map<List<human_file>>(list);
            di["list"] = list2;
            di["rows"] = rows;
            di["pages"] = pages;
            return di;
        }

        public async Task<List<human_file>> query_list1(FenYe ye)
        {
            var data = myDbContext.human_file.OrderBy(e => e.huf_id).ToList();
            ye.Rows = data.Count();
            ye.Pages = (ye.Rows - 1) / ye.PageSize + 1;
            List<Ehuman_file> list = new List<Ehuman_file>();
            await Task.Run(() =>
            {
                list = data.Skip((ye.CurrentPage - 1) * ye.PageSize)
                .Take(ye.PageSize)
                .ToList();
            });
            List<human_file> list2 = new List<human_file>();
            foreach (Ehuman_file item in list)
            {
                human_file a = new human_file()
                {
                    huf_id = item.huf_id,
                    human_id = item.human_id,
                    first_kind_id = item.first_kind_id,
                    first_kind_name = item.first_kind_name,
                    second_kind_id = item.second_kind_id,
                    second_kind_name = item.second_kind_name,
                    third_kind_id = item.third_kind_id,
                    third_kind_name = item.third_kind_name,
                    human_name = item.human_name,
                    human_address = item.human_address,
                    human_postcode = item.human_postcode,
                    human_pro_designation = item.human_pro_designation,
                    human_major_kind_id = item.human_major_kind_id,
                    human_major_kind_name = item.human_major_kind_name,
                    human_major_id = item.human_major_id,
                    hunma_major_name = item.hunma_major_name,
                    human_telephone = item.human_telephone,
                    human_mobilephone = item.human_mobilephone,
                    human_bank = item.human_bank,
                    human_account = item.human_account,
                    human_qq = item.human_qq,
                    human_email = item.human_email,
                    human_hobby = item.human_hobby,
                    human_speciality = item.human_speciality,
                    human_sex = item.human_sex,
                    human_religion = item.human_religion,
                    human_party = item.human_party,
                    human_nationality = item.human_nationality,
                    human_race = item.human_race,
                    human_birthday = item.human_birthday,
                    human_birthplace = item.human_birthplace,
                    human_age = item.human_age,
                    human_educated_degree = item.human_educated_degree,
                    human_educated_years = item.human_educated_years,
                    human_educated_major = item.human_educated_major,
                    human_society_security_id = item.human_society_security_id,
                    human_id_card = item.human_id_card,
                    remark = item.remark,
                    salary_standard_id = item.salary_standard_id,
                    salary_standard_name = item.salary_standard_name,
                    salary_sum = item.salary_sum,
                    demand_salaray_sum = item.demand_salaray_sum,
                    paid_salary_sum = item.paid_salary_sum,
                    major_change_amount = item.major_change_amount,
                    bonus_amount = item.bonus_amount,
                    training_amount = item.training_amount,
                    file_chang_amount = item.file_chang_amount,
                    human_histroy_records = item.human_histroy_records,
                    human_family_membership = item.human_family_membership,
                    human_picture = item.human_picture,
                    attachment_name = item.attachment_name,
                    check_status = item.check_status,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    lastly_change_time = item.lastly_change_time,
                    delete_time = item.delete_time,
                    recovery_time = item.recovery_time,
                    human_file_status = item.human_file_status,
                };
                list2.Add(a);
            }
            return list2;
        }
        //查看档案状态为正常的
        public async Task<List<human_file>> Humman_registerSlectFY(FenYe ye)
        {
            SqlParameter[] ps = {
            new SqlParameter(){  ParameterName="@pageSize",Value=ye.PageSize},
            new SqlParameter(){  ParameterName="@keyName",Value="huf_id"},
            new SqlParameter(){  ParameterName="@tabelName",Value="human_file"},
            new SqlParameter(){  ParameterName="@where",Value=ye.Where},
            new SqlParameter(){  ParameterName="@currentPage",Value=ye.CurrentPage},
            new SqlParameter(){  ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            new SqlParameter(){  ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int},
            };
            List<Ehuman_file> list = await myDbContext.human_file.FromSqlRaw("exec [dbo].[proc_Fenye] @pageSize, @keyName, @tabelName, @where, @currentPage,@rows out, @pages out", ps).ToListAsync();
            ye.Rows = (int)ps[5].Value;
            ye.Pages = (int)ps[6].Value;
            //把EO-DTO
            List<human_file> list2 = new List<human_file>();
            foreach (Ehuman_file item in list)
            {
                human_file t = new human_file()
                {
                    huf_id = item.huf_id,
                    human_id = item.human_id,
                    first_kind_id = item.first_kind_id,
                    first_kind_name = item.first_kind_name,
                    second_kind_id = item.second_kind_id,
                    second_kind_name = item.second_kind_name,
                    third_kind_id = item.third_kind_id,
                    third_kind_name = item.third_kind_name,
                    human_name = item.human_name,
                    human_address = item.human_address,
                    human_postcode = item.human_postcode,
                    human_pro_designation = item.human_pro_designation,
                    human_major_kind_id = item.human_major_kind_id,
                    human_major_kind_name = item.human_major_kind_name,
                    human_major_id = item.human_major_id,
                    hunma_major_name = item.hunma_major_name,
                    human_telephone = item.human_telephone,
                    human_mobilephone = item.human_mobilephone,
                    human_bank = item.human_bank,
                    human_account = item.human_account,
                    human_qq = item.human_qq,
                    human_email = item.human_email,
                    human_hobby = item.human_hobby,
                    human_speciality = item.human_speciality,
                    human_sex = item.human_sex,
                    human_religion = item.human_religion,
                    human_party = item.human_party,
                    human_nationality = item.human_nationality,
                    human_race = item.human_race,
                    human_birthday = item.human_birthday,
                    human_birthplace = item.human_birthplace,
                    human_age = item.human_age,
                    human_educated_degree = item.human_educated_degree,
                    human_educated_years = item.human_educated_years,
                    human_educated_major = item.human_educated_major,
                    human_society_security_id = item.human_society_security_id,
                    human_id_card = item.human_id_card,
                    remark = item.remark,
                    salary_standard_id = item.salary_standard_id,
                    salary_standard_name = item.salary_standard_name,
                    salary_sum = item.salary_sum,
                    demand_salaray_sum = item.demand_salaray_sum,
                    paid_salary_sum = item.paid_salary_sum,
                    major_change_amount = item.major_change_amount,
                    bonus_amount = item.bonus_amount,
                    training_amount = item.training_amount,
                    file_chang_amount = item.file_chang_amount,
                    human_histroy_records = item.human_histroy_records,
                    human_family_membership = item.human_family_membership,
                    human_picture = item.human_picture,
                    attachment_name = item.attachment_name,
                    check_status = item.check_status,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    lastly_change_time = item.lastly_change_time,
                    delete_time = item.delete_time,
                    recovery_time = item.recovery_time,
                    human_file_status = item.human_file_status,
                };
                list2.Add(t);
            }
            return list2;
        }
      
        /// <summary>
        /// /下拉框查职位分类下职位名称
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<config_major>> GetMajor(string ids)
        {
            List<Econfig_major> elist = new List<Econfig_major>();
            List<config_major> list = new List<config_major>();
            await Task.Run(() =>
            {
                elist = (from e in myDbContext.config_major
                         where e.major_kind_id == ids
                         select e).ToList();
            });
            list = mapper.Map<List<config_major>>(elist);
            return list;
        }

        /// <summary>
        /// 下拉框查职位分类全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<config_major_kind>> GetMojarKind()
        {
            List<Econfig_major_kind> elist = new List<Econfig_major_kind>();
            List<config_major_kind> list = new List<config_major_kind>();
            await Task.Run(() =>
            {
                elist = myDbContext.config_major_kind.ToList();
            });
            list = mapper.Map<List<config_major_kind>>(elist);
            return list;
        }
        /// <summary>
        /// 下拉框查薪酬标准全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<salary_standard>> GetSalary()
        {
            List<Esalary_standard> elist = new List<Esalary_standard>();
            List<salary_standard> list = new List<salary_standard>();
            await Task.Run(() =>
            {
                elist = (from e in myDbContext.salary_standard
                         where e.check_status == 2
                         select e).ToList();
            });
            list = mapper.Map<List<salary_standard>>(elist);
            return list;
        }
        /// <summary>
        /// //下拉框查一级下二级机构
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<config_file_second_kind>> GetSecond(string ids)
        {
            List<Econfig_file_second_kind> elist = new List<Econfig_file_second_kind>();
            List<config_file_second_kind> list = new List<config_file_second_kind>();
            await Task.Run(() =>
            {
                elist = (from e in myDbContext.config_file_second_kind
                         where e.first_kind_id == ids
                         select e).ToList();
            });
            list = mapper.Map<List<config_file_second_kind>>(elist);
            return list;
        }
        /// <summary>
        /// 下拉框查二级下三级机构
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<config_file_third_kind>> GetThird(string ids)
        {
            List<Econfig_file_third_kind> elist = new List<Econfig_file_third_kind>();
            List<config_file_third_kind> list = new List<config_file_third_kind>();
            await Task.Run(() =>
            {
                elist = (from e in myDbContext.config_file_third_kind
                         where e.second_kind_id == ids
                         select e).ToList();
            });
            list = mapper.Map<List<config_file_third_kind>>(elist);
            return list;
        }
        /// <summary>
        ///  //复核查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<human_file>> GetUOneHuman_file(int id)
        {
            List<Ehuman_file> elist = new List<Ehuman_file>();
            List<human_file> list = new List<human_file>();
            short ids = (short)id;
            await Task.Run(() =>
            {
                elist = (from e in myDbContext.human_file
                         where e.huf_id == +ids
                         select e).ToList();
                list = mapper.Map<List<human_file>>(elist);
            });
            return list;
        }
        /// <summary>
        /// 查询未复核的人力资源
        /// </summary>
        /// <param name="ye"></param>
        /// <returns></returns>
        public async Task<List<human_file>> GetHuman_fileOne(FenYe ye)
        {
            var data = myDbContext.human_file.OrderBy(e => e.huf_id).Where(e => e.check_status == 1).ToList();
            ye.Rows = data.Count();
            ye.Pages = (ye.Rows - 1) / ye.PageSize + 1;
            List<Ehuman_file> list = new List<Ehuman_file>();
            await Task.Run(() =>
            {
                list = data.Skip((ye.CurrentPage - 1) * ye.PageSize)
                .Take(ye.PageSize)
                .ToList();
            });
            List<human_file> list2 = new List<human_file>();
            foreach (Ehuman_file item in list)
            {
                human_file a = new human_file()
                {
                    huf_id = item.huf_id,
                    human_id = item.human_id,
                    first_kind_id = item.first_kind_id,
                    first_kind_name = item.first_kind_name,
                    second_kind_id = item.second_kind_id,
                    second_kind_name = item.second_kind_name,
                    third_kind_id = item.third_kind_id,
                    third_kind_name = item.third_kind_name,
                    human_name = item.human_name,
                    human_address = item.human_address,
                    human_postcode = item.human_postcode,
                    human_pro_designation = item.human_pro_designation,
                    human_major_kind_id = item.human_major_kind_id,
                    human_major_kind_name = item.human_major_kind_name,
                    human_major_id = item.human_major_id,
                    hunma_major_name = item.hunma_major_name,
                    human_telephone = item.human_telephone,
                    human_mobilephone = item.human_mobilephone,
                    human_bank = item.human_bank,
                    human_account = item.human_account,
                    human_qq = item.human_qq,
                    human_email = item.human_email,
                    human_hobby = item.human_hobby,
                    human_speciality = item.human_speciality,
                    human_sex = item.human_sex,
                    human_religion = item.human_religion,
                    human_party = item.human_party,
                    human_nationality = item.human_nationality,
                    human_race = item.human_race,
                    human_birthday = item.human_birthday,
                    human_birthplace = item.human_birthplace,
                    human_age = item.human_age,
                    human_educated_degree = item.human_educated_degree,
                    human_educated_years = item.human_educated_years,
                    human_educated_major = item.human_educated_major,
                    human_society_security_id = item.human_society_security_id,
                    human_id_card = item.human_id_card,
                    remark = item.remark,
                    salary_standard_id = item.salary_standard_id,
                    salary_standard_name = item.salary_standard_name,
                    salary_sum = item.salary_sum,
                    demand_salaray_sum = item.demand_salaray_sum,
                    paid_salary_sum = item.paid_salary_sum,
                    major_change_amount = item.major_change_amount,
                    bonus_amount = item.bonus_amount,
                    training_amount = item.training_amount,
                    file_chang_amount = item.file_chang_amount,
                    human_histroy_records = item.human_histroy_records,
                    human_family_membership = item.human_family_membership,
                    human_picture = item.human_picture,
                    attachment_name = item.attachment_name,
                    check_status = item.check_status,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    lastly_change_time = item.lastly_change_time,
                    delete_time = item.delete_time,
                    recovery_time = item.recovery_time,
                    human_file_status = item.human_file_status,
                };
                list2.Add(a);
            }
            return list2;
        }

        public async Task<int> UpdHuman_file(human_file hf)
        {
            int num = 0;
            Ehuman_file eh = myDbContext.human_file.Find(hf.huf_id);
            eh.human_id = hf.human_id;
            eh.first_kind_id = hf.first_kind_id;
            eh.first_kind_name = hf.first_kind_name;
            eh.second_kind_id = hf.second_kind_id;
            eh.second_kind_name = hf.second_kind_name;
            eh.third_kind_id = hf.third_kind_id;
            eh.third_kind_name = hf.third_kind_name;
            eh.human_name = hf.human_name;
            eh.human_address = hf.human_address;
            eh.human_postcode = hf.human_postcode;
            eh.human_pro_designation = hf.human_pro_designation;
            eh.human_major_kind_id = hf.human_major_kind_id;
            eh.human_major_kind_name = hf.human_major_kind_name;
            eh.human_major_id = hf.human_major_id;
            eh.hunma_major_name = hf.hunma_major_name;
            eh.human_telephone = hf.human_telephone;
            eh.human_mobilephone = hf.human_mobilephone;
            eh.human_bank = hf.human_bank;
            eh.human_account = hf.human_account;
            eh.human_qq = hf.human_qq;
            eh.human_email = hf.human_email;
            eh.human_hobby = hf.human_hobby;
            eh.human_speciality = hf.human_speciality;
            eh.human_sex = hf.human_sex;
            eh.human_religion = hf.human_religion;
            eh.human_party = hf.human_party;
            eh.human_nationality = hf.human_nationality;
            eh.human_race = hf.human_race;
            eh.human_birthday = hf.human_birthday;
            eh.human_birthplace = hf.human_birthplace;
            eh.human_age = hf.human_age;
            eh.human_educated_degree = hf.human_educated_degree;
            eh.human_educated_years = hf.human_educated_years;
            eh.human_educated_major = hf.human_educated_major;
            eh.human_society_security_id = hf.human_society_security_id;
            eh.human_id_card = hf.human_id_card;
            eh.remark = hf.remark;
            eh.salary_standard_id = hf.salary_standard_id;
            eh.salary_standard_name = hf.salary_standard_name;
            eh.salary_sum = hf.salary_sum;
            eh.demand_salaray_sum = hf.demand_salaray_sum;
            eh.paid_salary_sum = hf.paid_salary_sum;
            eh.major_change_amount = (short)(eh.major_change_amount + 1);
            eh.bonus_amount = hf.bonus_amount;
            eh.training_amount = hf.training_amount;
            eh.file_chang_amount = hf.file_chang_amount;
            eh.human_histroy_records = hf.human_histroy_records;
            eh.human_family_membership = hf.human_family_membership;
            eh.human_picture = hf.human_picture;
            eh.attachment_name = hf.attachment_name;
            eh.check_status = hf.check_status;
            eh.register = hf.register;
            eh.checker = hf.checker;
            eh.changer = hf.changer;
            eh.regist_time = hf.regist_time;
            eh.check_time = hf.check_time;
            eh.change_time = hf.change_time;
            eh.lastly_change_time = hf.lastly_change_time;
            eh.delete_time = hf.delete_time;
            eh.recovery_time = hf.recovery_time;
            eh.human_file_status = hf.human_file_status;
            num = await myDbContext.SaveChangesAsync();
            return num;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="hf"></param>
        /// <returns></returns>
        public async Task<int> DelFalse(human_file hf)
        {
            int num = 0;
            Ehuman_file eh = myDbContext.human_file.Find(hf.huf_id);
            eh.human_file_status = hf.human_file_status;
            num = await myDbContext.SaveChangesAsync();
            return num;
        }
    }
    
}
